/**
 * 预制体UI的添加,移除操作的管理组件
 */

window.AddPrefabHelper = cc.Class({
    extends: cc.Component,

    properties: () => ({


    }),

    statics: {

        createPrefab: function(prefab){
            let prefabNode = null;
            if (cc.supportJit) {
                cc.supportJit = false;
                prefabNode = cc.instantiate(prefab);
                cc.supportJit = true;
            }
            else {
                // 如果是特殊的不支持 JIT 的平台，则不需要优化，也不能强制设置 cc.supportJit 为 true
                prefabNode = cc.instantiate(prefab);
            }
            return prefabNode;
        },

        addPrefabUI: function(url, parent, zOrder, callback){
            AddPrefabHelper.addShieldNode();
            let self = this;
            UIHelper.loadRes(url, cc.Prefab, function(prefab){
                let prefabUI = self.createPrefab(prefab);
                parent.addChild(prefabUI, zOrder);
                AddPrefabHelper.removeShieldNode();
                if(callback)
                {  
                    callback(prefabUI);
                }
            });
        },

        //添加一个全屏屏蔽触摸的node
        addShieldNode: function(callback){
            if(this.isHaveShieldNode())
            {
                if(callback)
                {
                    callback(cc.director.getScene().getChildByName('shieldNode'));
                }
                return;
            }
            let winSize = cc.director.getWinSize();
            let shieldNode = new cc.Node('shieldNode');
            shieldNode.anchorX = 0;
            shieldNode.anchorY = 0;
            shieldNode.setContentSize(winSize);
            cc.director.getScene().addChild(shieldNode, 100000);
            shieldNode.on('touchstart', function (event) {
                event.stopPropagation();
                return false;
            });

            if(callback)
            {
                callback(shieldNode);
            }
        },
    },
});


module.exports = AddPrefabHelper;